package com.tianyang.yukuaifuzs.business.trade.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseEditActivity;
import com.tianyang.yukuaifuzs.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.TradeConfig;
import com.tianyang.yukuaifuzs.business.trade.contract.UpdateSettleCardContract;
import com.tianyang.yukuaifuzs.business.trade.presenter.UpdateSettleCardPresenter;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsMgr;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：修改结算卡
 * Created by xyj on 2024/3/11
 */
public class UpdateSettleCardActivity extends BaseEditActivity implements UpdateSettleCardContract.View {

    private String[] permissions = new String[]{PermissionsConstant.CAMERA, PermissionsConstant.WRITE_STORAGE, PermissionsConstant.READ_STORAGE};

    @BindView(R.id.realNameTv)
    TextView realNameTv;
    @BindView(R.id.bankPhoneEt)
    EditText bankPhoneEt;
    @BindView(R.id.bankCardEt)
    EditText bankCardEt;
    @BindView(R.id.bankBranchNameTv)
    TextView bankBranchNameTv;
    @BindView(R.id.bankFrontImgSdv)
    SimpleDraweeView bankFrontImgSdv;
    @BindView(R.id.bankBranchNameLl)
    LinearLayout bankBranchNameLl;

    private UpdateSettleCardContract.Presenter mPresenter;

    private SettleBankOutput mOutput;

    private String merchantType;

    private boolean isAgent = false;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_update_settle_card;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("结算卡更改");
        isAgent = getIntent().getBooleanExtra(TradeConfig.INTENT_IS_AGENT, false);
        if (isAgent) {
            bankBranchNameLl.setVisibility(View.GONE);
        }
        mPresenter = new UpdateSettleCardPresenter(this, isAgent);
        mOutput = new SettleBankOutput();
    }

    @OnClick({R.id.captureIv, R.id.bankBranchNameTv, R.id.bankFrontImgRfl, R.id.confirmTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.captureIv:
            case R.id.bankFrontImgRfl:
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(CommonConfig.TYPE_CRDFROPIC);
                break;
            case R.id.bankBranchNameTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, merchantType,
                        AuthConfig.MCC_TYPE_BANK, "", CommonConfig.REQUESTCODE_UPDATE_BUSINESS_BANK);
                break;
            case R.id.confirmTv:
                if (checkSettleCard()) {
                    mPresenter.modifyUserSettlementCard(mOutput);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            realNameTv.setText(VerifyUtil.checkString(output.getAgentName()));
            merchantType = output.getMerchantType() + "";
        }
    }

    @Override
    public void showPhotoView(String urlStr, String photoType) {
        if (TextUtils.equals(CommonConfig.TYPE_CRDFROPIC, photoType)) {
            bankFrontImgSdv.setImageURI(Uri.parse(urlStr));
            mOutput.setBankcardpic(urlStr);
        }
    }

    @Override
    public void showBankCardView(BankRegOutput output) {
        if (output != null) {
            mOutput.setBankName(output.getBank_name());
            if (!TextUtils.isEmpty(output.getBank_card_number())) {
                String bankCardId = output.getBank_card_number().replaceAll(" ", "");
                bankCardEt.setText(bankCardId);
                mOutput.setBankCardId(bankCardId);
            }
        }
    }

    private boolean checkSettleCard() {
        if (TextUtils.isEmpty(mOutput.getBankcardpic())) {
            ToastUtil.show("请上传银行卡卡号照");
            return false;
        }
        if (TextUtils.isEmpty(bankPhoneEt.getText())) {
            ToastUtil.show("请输入银行预留手机号");
            SoftInputUtil.openKeybord(bankPhoneEt, getContext());
            return false;
        } else if (VerifyUtil.checkPhoneNumber(bankPhoneEt.getText())) {
            ToastUtil.show("请输入正确的手机号");
            SoftInputUtil.openKeybord(bankPhoneEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(bankCardEt.getText())) {
            ToastUtil.show("请输入银行卡号");
            SoftInputUtil.openKeybord(bankCardEt, getContext());
            return false;
        }
        if (!isAgent && TextUtils.isEmpty(mOutput.getBankBranchNo())) {
            ToastUtil.show("请选择开户支行");
            return false;
        }
        mOutput.setPhone(bankPhoneEt.getText().toString());
        mOutput.setBankCardId(bankCardEt.getText().toString());
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_UPDATE_BUSINESS_BANK) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                BusinessOutput output = (BusinessOutput) data.getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO);
                if (output != null) {
                    bankBranchNameTv.setText(VerifyUtil.checkString(output.getBranchBankName()));
                    mOutput.setBankBranchName(output.getBranchBankName());
                    mOutput.setBankBranchNo(output.getBranchNo());
                }
            }
        } else {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPermissionGranted(List<String> permission) {
        if (PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
            mPresenter.showSelectImgSourceView(CommonConfig.TYPE_CRDFROPIC);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(UpdateSettleCardContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
